package controller;
import model.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/search_book")
public class search_book extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String word=req.getParameter("word");
        String page =req.getParameter("page");
        MySql mySql=new MySql();
        /*if (req.getSession().getAttribute("user")!=null){
            req.setAttribute("userinfo",mySql.getUserByName(String.valueOf(req.getSession().getAttribute("user"))));
        }*/
        if(word!=null){
            req.setAttribute("search_book",mySql.getBooksBySearch(word,Integer.parseInt(page)));
        }
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            /*List<model.Book> book=mySql.getBooksBySearch(word,Integer.parseInt(page));
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(book);
            resp.getWriter().print(json);*/
    }
}
